package com.taomu.app.zongchouhuli.utils;

import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;


/**
 * Created by kezhong.
 * E-Mail:396926020@qq.com
 * on 2016/10/22 15:44
 */

public class ListViewUtils {

    /**
     * 给ListView设置一个默认的空界面
     *
     * @param listView
     * @return 空界面
     */
    public static View setDefaultEmpty(AbsListView listView) {
        return setEmptyView(listView, R.layout.searchnocontent);
    }

    /**
     * 给ListView设置ListView内容为空时显示的界面
     *
     * @param listView    要设置的ListView
     * @param emptyViewId 空内容的界面的id
     * @return 空界面
     */
    public static View setEmptyView(AbsListView listView, @LayoutRes int emptyViewId) {
        if (listView == null || emptyViewId == 0) {
            return null;
        }
        View emptyView = View.inflate(listView.getContext(), emptyViewId, null);
        return setEmptyView(listView, emptyView);
    }

    /**
     * 给ListView设置ListView内容为空时显示的界面
     *
     * @param listView  要设置的ListView
     * @param emptyView 空内容的View
     * @return 空界面
     */
    public static View setEmptyView(AbsListView listView, View emptyView) {
        if (listView == null || emptyView == null) {
            return null;
        }
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        ((ViewGroup) listView.getParent()).addView(emptyView, lp);
        listView.setEmptyView(emptyView);
        return emptyView;
    }

    /**
     * 给RecyclerView设置一个默认的空界面
     *
     * @param recyclerView
     * @return 空界面
     */
    public static View setDefaultEmpty(EmptyRecyclerView recyclerView) {
        return setEmptyView(recyclerView, R.layout.searchnocontent);
    }

    /**
     * 给RecyclerView设置RecyclerView内容为空时显示的界面
     *
     * @param recyclerView 要设置的ListView
     * @param emptyViewId  空内容的界面的id
     * @return 空界面
     */
    public static View setEmptyView(EmptyRecyclerView recyclerView, @LayoutRes int emptyViewId) {
        if (recyclerView == null || emptyViewId == 0) {
            return null;
        }
        View emptyView = View.inflate(recyclerView.getContext(), emptyViewId, null);
        return setEmptyView(recyclerView, emptyView);
    }

    /**
     * 给RecyclerView添加空界面
     *
     * @param recyclerView
     * @param emptyView
     * @return
     */
    public static View setEmptyView(EmptyRecyclerView recyclerView, View emptyView) {
        if (recyclerView == null || emptyView == null) {
            return emptyView;
        }
        int recyclerViewIndex = ((ViewGroup)recyclerView.getParent()).indexOfChild(recyclerView);
        ((ViewGroup)recyclerView.getParent()).addView(emptyView, recyclerViewIndex, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        recyclerView.setEmptyView(emptyView);
        return emptyView;
    }
}
